Validate MessagePack enum values#38204
Conversation
| baseFormatter = StandardResolver.Instance.GetFormatter<TBase>(); | ||
| baseFormatter = StandardResolver.Instance.GetFormatterWithVerify<TBase>(); |
There was a problem hiding this comment.
The "WithVerify" part of this checks that the formatter is not null. Was just looking through the MsgPack source and this is kind of how they do it, plus I needed the non-nullability.
| if (Enum.IsDefined(typeof(T), value)) | ||
| return; |
There was a problem hiding this comment.
[Flags] enums need to be exempted from this because there not being "defined" is actually a feature if you have a union of 2 or more flags.
This is actually relevant because we have one of those that goes over the wire to spectator server and it's ReplayButtonState.
diff --git a/osu.Game.Tests/Online/TestMultiplayerMessagePackSerialization.cs b/osu.Game.Tests/Online/TestMultiplayerMessagePackSerialization.cs
index 6891b214d0..08e3e3a660 100644
--- a/osu.Game.Tests/Online/TestMultiplayerMessagePackSerialization.cs
+++ b/osu.Game.Tests/Online/TestMultiplayerMessagePackSerialization.cs
@@ -7,6 +7,7 @@
using osu.Game.Online;
using osu.Game.Online.Multiplayer;
using osu.Game.Online.Multiplayer.MatchTypes.TeamVersus;
+using osu.Game.Replays.Legacy;
namespace osu.Game.Tests.Online
{
@@ -86,6 +87,9 @@ public void TestEnumValidation()
MessagePackSerializer.Deserialize<SimpleEnum?>(MessagePackSerializer.Serialize((SimpleEnum?)null, SignalRUnionWorkaroundResolver.OPTIONS), SignalRUnionWorkaroundResolver.OPTIONS));
Assert.Throws<MessagePackSerializationException>(() =>
MessagePackSerializer.Deserialize<SimpleEnum?>(MessagePackSerializer.Serialize((SimpleEnum)100, SignalRUnionWorkaroundResolver.OPTIONS), SignalRUnionWorkaroundResolver.OPTIONS));
+
+ Assert.DoesNotThrow(() =>
+ MessagePackSerializer.Deserialize<ReplayButtonState>(MessagePackSerializer.Serialize(ReplayButtonState.Left1 | ReplayButtonState.Right2, SignalRUnionWorkaroundResolver.OPTIONS), SignalRUnionWorkaroundResolver.OPTIONS));
}
public enum SimpleEnum
The above fails when run.
There was a problem hiding this comment.
I'm just gonna go ahead and close this PR for now... Detecting flags enums would require reflection, and at which point I'd rather be looking towards other more full-fledged validation mechanisms.
No description provided.